<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : AdminMenus.php
 * Created on : 2020-7-25 12:44:47
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\model;

use think\Model;

/**
 * 
 * Class AdminMenus
 * Tiger <1192851302@qq.com> 2020-7-25 12:44:47
 */
class AdminMenus extends Model {

    protected $name = 'admin_menus';

    /**
     * 
     * @param type $where
     * @return type
     */
    public static function getAlls($where = []) {
        return self::where($where)->order('sort desc,id asc')->select()->toArray();
    }

    public static function getList() {
        $menus = self::getAlls();
        // 第一层
        $menuInfo = self::getByParentId($menus);
        // 第二层
        foreach ($menuInfo as &$itRef) {
            $menuInfoLv2 = self::getByParentId($menus, $itRef['id']);
            if (empty($menuInfoLv2)) {
                continue;
            }
            // 第三层
            foreach ($menuInfoLv2 as &$itRefLv2) {
                $menuInfoLv3 = self::getByParentId($menus, $itRefLv2['id']);
                if (empty($menuInfoLv3)) {
                    continue;
                }
                // 第三层
                $itRefLv2['list'] = $menuInfoLv3;
            }
            // 第二层
            $itRef['list'] = $menuInfoLv2;
        }
        return $menuInfo;
    }

    public static function getByParentId(&$menus, $parentId = 0) {
        $needMenus = array();
        foreach ($menus as $key => $it) {
            if ($it['pid'] == $parentId) {
                $it['name'] = $it['jump'];
                $needMenus[] = $it;
                unset($menus[$key]);
            }
        }
        return $needMenus;
    }

}
